<template>
    <!-- 设置通知消息 -->
    <el-dialog title="高级设置" v-model="dialogVisible" width="550px" append-to-body>
        <notice-conf @changeFlowMsgSet="handleFlowMsgSet" />
        <template #footer>
            <div class="dialog-footer">
                <el-button type="primary" @click="submitDialog">确认</el-button>
                <el-button @click="closeDialog">关闭</el-button>
            </div>
        </template>
    </el-dialog>
</template>
<script>
import noticeConf from "@/components/WorkflowNew/drawer/noticeConfig/index.vue";
export default {
  components: {
    msgIcon,
    SetSeniorMsg,
    ViewFormDrawer
  },
  props: {
    visible: {
        type: Boolean,
        default: false,
    },
    
  },
  data () {
    return {
        flowMasgVo:[],
        dialogVisible:false

    }
  },
  watch: {
    dialogVisible: {
      immediate: true,
      handler(val) {
        if (val) {
            this.checkedMsgSendTypeList = this.formMsgData.processNotices.filter(c => c.active).map(item => {
                return item.id;
            });
            this.msgTableData = this.formMsgData.templateVos || [];
        }
      }
    },
    
  },
  mounted(){
    
  },
  methods: {
    
    submitDialog() {
        let params = {
            processKey: this.formMsgData.formCode,
            notifyTypeIds: this.checkedMsgSendTypeList,
            templateVos: this.msgTableData
        }
        saveTaskMgmt(params).then(res => {
            if (res && res.code == 200) {
                this.$modal.msgSuccess("保存成功");
                this.$emit("refresh", this.formMsgData.flowType);
                closeDialog();
            } else {
                this.$modal.msgError("保存失败" + res.errMsg);
            }
        }).catch(err => {
            console.log(err);
        });
    },

    openSeniorSet() {
        this.noticeSeniorSetShow = true;
    },
    /**消息设置 */
   handleFlowMsgSet(data){
        if (this.isEmpty(data)) return;

        const propsToCheck = ['messageSendTypeList', 'event', 'templateId', 'informIdList'];
        if (this.hasEmptyValue(data, propsToCheck)) {
            this.$modal.msgError("请选择完整的消息设置");
            return;
        }
        const index = this.msgTableData.findIndex(item => item.event === data.event);
        if (index !== -1) {
            // 如果找到具有相同 event 的条目，则更新该条目
            this.msgTableData[index] = { ...this.msgTableData[index], ...data };
        } else {
            // 如果没有找到，则添加新的条目
           this.msgTableData.push(data);
        }
    },

    /**消息设置 */
    handleDelete (row){
        this.msgTableData= this.msgTableData.filter(x => x.event !== row.event)
    },

    getNoticeUserType(param){
        if (this.isEmptyArray(param)) return '';
        let ret = noticeUserList.filter(item => {
            return param.includes(item.value);
        })?.map(item => item.label).join(', ');
        return ret;
    },

    getEventType(param){
        if (this.isEmptyArray(param)) return '';
        return eventTypeList.filter(item => {
            return item.id == param;
        })?.map(item => item.name).join(', ');
    },

    /** 取消操作添加条件模板表单 */
    closeDialog() {
        this.dialogVisible=fasle
        emits("update:visible", false);
    }
  }
}
</script>
